
var sidx = "id";
var sord = "desc";
var curr = 1;
var contextPath = $('input[name=contextPath]').val().replace("/main","");
$(function() 
{
	 var winwidth= $(window).width();
	 $("#divlist").width(winwidth);
	 var winheight= $(window).height()-50;
	 $("#divlist").height(winheight);
	 if(winwidth-50>1700)
		   $("#thetable").width(winwidth-50);
	 
	loadData();
	

	
});
layui.use('form', function() {
	var $ = layui.jquery, form = layui.form();
	// 全选
	form.on('checkbox(allChoose)', function(data) {
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	// 单个取消选中
	form.on('checkbox(choose)', function(data) {
		var flag = true;
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
		child.each(function(index, item) {
			if (!item.checked) {
				flag = false;
				return false;
			}
		});
		$('input[name=allChoose]').prop('checked', flag);
		form.render('checkbox');
	});
	form.render();
});
function sortname(obj, tname) {
	sidx = tname;
	$("i.fa").removeClass("fa-sort-asc");
	$("i.fa").removeClass("fa-sort-desc");
	if (sord == "desc") {
		sord = "asc"
		$(obj).find('i').addClass("fa-sort-asc");
	} else {
		sord = "desc";
		$(obj).find('i').addClass("fa-sort-desc");
	}
	loadData();
}
function loadData() {
	
	$.ajax({
		url : contextPath + "/sys/sysmdevicemanage/getlist",
		type : 'post',
		async : true,
		/*contentType:"application/json",*/
		dataType : 'json',
		data :{
			rows : 15,
			curpage : curr,
			order : sidx,
			sortspecies : sord,
			search:$("#search").val()
		},
		success : function(r) {
			if (r && r.success) {
				$('#tbody').empty();
				var showhtml = '';
				if (r.obj.list.length == 0) 
				{
					showhtml += '<tr>';
					showhtml += '<td colspan="22"  align="center">暂无数据</td>';
					showhtml += '</tr>';
				} 
				else
				{
					for (var i = 0; i < r.obj.list.length; i++) 
					{
						var model = r.obj.list[i];
						showhtml += "<tr ondblclick='showmemberdetail("+model.id+")'>";
						showhtml += "<td style='text-align:center;'><input type='checkbox' name='oneChoose' fid='"+model.id+"' iscommentstate='"+model.iscomment+"' lay-skin='primary' lay-filter='choose'></td>";
						showhtml += '<td  align="center">' +((i+1)+(curr-1)*15)+ '</td>';
						showhtml += "<td style='text-align:center;'>" + model.number + "</td>";
						showhtml += "<td style='text-align:center;'>" + model.name+"</td>";
						showhtml += "<td style='text-align:center;'>" + model.model + "</td>";
						showhtml += "<td style='text-align:center;'>" + model.pid+"</td>";
						showhtml += "<td style='text-align:center;'>" + (model.manufacturer==1?"LEICA":"TRIMBLE") + "</td>";
						showhtml += "<td style='text-align:center;'>" + (model.state==0?"正常":model.state==1?"故障":"报废")+"</td>";
						showhtml += "<td style='text-align:center;'>" + model.comment+"</td>";
						showhtml += '</tr>';
					}
				}
			
				$('#tbody').append(showhtml);

				if (r.obj.pages >= 1) {
					layui.use([ 'laypage', 'layer' ], function() {
						var laypage = layui.laypage, layer = layui.layer;
						// 调用分页
						
						
						laypage({
							cont : 'divpage',
							curr : curr, // 当前页
							skip : true, // 是否开启跳页
							pages : r.obj.pages, // 得到总页数
							isshowtotal:true,// 显示汇总信息                                                       
							totalrecord:r.obj.total,// 显示汇总信息
							jump : function(obj, first) { // 触发分页后的回调
								if (!first) { // 点击跳页触发函数自身，并传递当前页：obj.curr
									curr = obj.curr;
									loadData();
								}
							}
						});

					});

				} else {
					//$("#divpage").hide();
				}

			}
		  	layui.use('form', function() 
			  {// 重新渲染表单
						var $ = layui.jquery, form = layui.form();
						$('input[name=allChoose]').prop('checked', false);
						form.render('checkbox');
						//form.render(); //整个渲染也可以

			});
		
		}
	});


};

	
	/**
	 * 获取选中列的id
	 * @returns {Array}
	 */
	function getchecked(){
		var chk_value =[];
		$('input[name="oneChoose"]:checked').each(function(){
			chk_value.push($(this).attr('fid'));
		});
		return chk_value;
	}


	
	
	/**
	 * 打开添加页面
	 */
	function addmd(){
			layer.open({
				type: 2,
				area: ['600px', '600px'],
				closeBtn: 1, //不显示关闭按钮
				fixed: false, //不固定
				title: '添加测量仪器', //不显示标题
				maxmin: true,
				end:function(){
					loadData();
				},
				content: "/sys/sysmdevicemanage/fdaddmd"
			});
	}
	
	/**
	 * 打开修改页面
	 */
	function updatemd(){
		var checked_value = getchecked();
		if(checked_value && checked_value.length==1){
			layer.open({
				type: 2,
				area: ['600px', '600px'],
				closeBtn: 1, //不显示关闭按钮
				fixed: false, //不固定
				title: '修改测量仪器', //不显示标题
				maxmin: true,
				end:function(){
					loadData();
				},
				content: "/sys/sysmdevicemanage/fdupdatemd?id="+checked_value,
			});
		}else{
			layer.alert("请选择有效行！");
		}
	}
	
	
	/**
	 * 删除
	 */
	function deletemd(){
		var checked_value = getchecked();
		if(checked_value && checked_value.length>0){
			layer.confirm("已选中"+checked_value.length+"项！确定删除？", {
				btn: ['确定','取消'] //按钮
			}, function(){
				$.ajax({
					type:'post',
					url:contextPath + "/sys/sysmdevicemanage/deletemd?ids="+checked_value,
					dataType:'json',
					success:function(result){
						if(result.success){
							layer.alert("删除成功");
							loadData();
						}else{
							layer.alert("删除失败");
							
						}
						
					}
				})
			});
		}else{
			layer.alert("请至少勾选一条会员信息！");
		}
	}
	